package com.flea.hclv.service.impl;

import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HAddressMapper;
import com.flea.hclv.model.dao.HResourceRelationMapper;
import com.flea.hclv.model.dao.HSpotMapper;
import com.flea.hclv.model.dao.HlineSpotRelationMapper;
import com.flea.hclv.model.entity.HAddress;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.model.entity.HSpot;
import com.flea.hclv.model.entity.vo.ImageVo;
import com.flea.hclv.model.entity.vo.LineSpotRelation;
import com.flea.hclv.model.entity.vo.SpotVo;
import com.flea.hclv.service.HSpotService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HSpotServiceImpl implements HSpotService{

    @Autowired
    private HSpotMapper mapper;
    @Autowired
    private HAddressMapper hAddressMapper;
    @Autowired
    private HResourceRelationMapper hResourceRelationMapper;
    @Autowired
    private HlineSpotRelationMapper hlineSpotRelationMapper;

    @Override
    public ServerResponse<FleaPage<HSpot>> selectMySpotByDeptId(String deptUuid, int curPage, int pageSize, String spotName, String province, String city, String area, String level) {
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        if (StringUtils.isNotBlank(spotName)){
            StringBuilder sb = new StringBuilder();
            sb.append("%");
            sb.append(spotName);
            sb.append("%");
            spotName = String.valueOf(sb);
        }
        List<HAddress> addresses;
        List<HSpot> spots = null;
        if (StringUtils.isNotBlank(province)||StringUtils.isNotBlank(city)||StringUtils.isNotBlank(area)){
            addresses = hAddressMapper.selectByParams(province,city,area);
            spots = mapper.selectByAddressAndDeptId(deptUuid,addresses);
        }
        List<HSpot> hSpotList = mapper.selectMySpotByDeptId(deptUuid,rowBounds,spotName,level,spots);
        FleaPage<HSpot> fleaPage = new FleaPage<>(hSpotList);
        fleaPage.setCurPageData(hSpotList);
        return ServerResponse.createBySuccess(fleaPage);
    }

    @Override
    public ServerResponse<List<HSpot>> selectByDeptId(String deptUuid) {
        List<HSpot> data = mapper.selectByDeptId(deptUuid);
//        List<HSpot> list = Lists.newArrayList();
        for (HSpot entity : data){
            List<HResourceRelation> hResourceRelations = hResourceRelationMapper.selectByUuid(entity.getId());
            if (CollectionUtils.isNotEmpty(hResourceRelations)){
                HResourceRelation image = hResourceRelations.get(0);
                entity.setImageId(image.getId());
                entity.setImagePath(image.getPath());
            }

        }
        return ServerResponse.createBySuccess(data);
    }

    @Override
    public ServerResponse<List<HSpot>> selectByLineId(String lineId) {
        List<LineSpotRelation> lineSpotRelations = hlineSpotRelationMapper.selectByLineId2(lineId);
        List<HSpot> spotList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(lineSpotRelations)){
            for (LineSpotRelation entity : lineSpotRelations){
                HSpot spot = mapper.selectByPrimaryKey(entity.getSpotId());
                if (spot!=null){
                    List<HResourceRelation> list = hResourceRelationMapper.selectByUuid(spot.getId());
                    if (CollectionUtils.isNotEmpty(list)){
                        HResourceRelation image = list.get(0);
                        spot.setImageId(image.getId());
                        spot.setImagePath(image.getPath());
                    }
                    spotList.add(spot);
                }
            }
        }
        return ServerResponse.createBySuccess(spotList);
    }

    @Override
    public List<HSpot> selectAllSpots() {
        return mapper.selectAllSpots();
    }

    @Override
    public boolean deleteByIds(List<String> idList, String userId) {
        int result = mapper.deleteIds(idList,userId);
        //todo 现在景点做的软删除，没有删除关联关系
        return result>0;
    }

    @Override
    public List<SpotVo> selectSpotInfo(String userId) {
        OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
        List<HSpot> spotList;
        String deptId = HConstant.HCLV_ADMIN_ADMIN;
        if (dept!=null){
            deptId = dept.getDeptUuid();
        }
        spotList = mapper.selectByDeptId(deptId);
        return assembleSpotVo(spotList);
    }

    private List<SpotVo> assembleSpotVo(List<HSpot> hSpotList){
        List<SpotVo> spotVos = new ArrayList<>();
        if (hSpotList.size()>0){
          for (HSpot spot:hSpotList){
              SpotVo spotVo = new SpotVo();
              List<HResourceRelation> hResourceRelations =  hResourceRelationMapper.selectByUuid(spot.getId());
              List<ImageVo> imageVos = new ArrayList<>();
              for (HResourceRelation hResourceRelation:hResourceRelations){
                  ImageVo imageVo = new ImageVo();
                  imageVo.setPath(hResourceRelation.getPath());
                  imageVo.setImageId(hResourceRelation.getId());
                  imageVos.add(imageVo);
              }
              spotVo.setSpotId(spot.getId());
              spotVo.setName(spot.getName());
              spotVo.setDescription(spot.getDescription());
              spotVo.setImages(imageVos);
              spotVos.add(spotVo);
          }
        }
        return spotVos;
    }

    @Override
    public boolean delete(String userId, String spotId) {
        int result;
        if (StringUtil.isEmpty(userId)||StringUtil.isEmpty(spotId)){
            return false;
        }
        String valid = "N";
        result = mapper.updateValidById(spotId,valid);
        return result>0;
    }

    @Override
    public FleaPage<HSpot> selectAll(int curPage, int pageSize, String spotName, String province, String city, String area, String level) {
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        List<HSpot> hSpotList = mapper.selectAll(rowBounds,spotName,province, city, area, level);
        return new FleaPage<>(hSpotList);
    }

    @Override
    public boolean saveOrUpdate(HSpot entity, String userId, HAddress address) {
        String id = entity.getId();
        entity.setModifyUser(userId);
        int result;
        if (StringUtil.isEmpty(id)) {
            id = FleaStringUtils.getGenerateUUID();
            entity.setId(id);
            entity.setCreateUser(userId);
            OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
            if (dept != null) {
                entity.setUuid(dept.getDeptUuid());
            }
            result = mapper.insertSelective(entity);

            if (StringUtil.isEmpty(address.getId())&&!StringUtil.isEmpty(entity.getId())){
                id = FleaStringUtils.getGenerateUUID();
                address.setId(id);
                address.setUuid(entity.getId());
                address.setCreateUser(userId);
                address.setModifyUser(userId);
                result = hAddressMapper.insertSelective(address);
                return result > 0;
            }
        }else {
            address.setUuid(entity.getId());
            address.setId(address.getAddRessId());
            hAddressMapper.updateByPrimaryKeySelective(address);
            result = mapper.updateByPrimaryKeySelective(entity);
        }
        return result > 0;
    }

}
